﻿using System;
using System.Collections.Generic;
using Socium.Domain.Collections;
using Socium.Domain.Persons;
using Socium.Domain.Tests;
using Socium.Domain.Tests.Sociometric;
using Socium.Domain.Tests.Testings;

namespace Socium.Domain {
	[Serializable]
	public class Database {
		readonly ICollection<Person> _persons = new LinkedList<Person>();
		readonly ICollection<Group> _groups = new LinkedList<Group>();
		
		readonly ICollection<Test> _tests = new LinkedList<Test>();
		readonly ICollection<Procedure> _procedures = new LinkedList<Procedure>(); 

		readonly ICollection<Testing> _testings = new LinkedList<Testing>();

		public ICollection<Person> Persons {
			get { return _persons; }
		}

		public ICollection<Group> Groups {
			get { return _groups; }
		} 

		public ICollection<Test> Tests {
			get { return _tests; }
		}

		public ICollection<Procedure> Procedures {
			get { return _procedures; }
		}

		public ICollection<Testing> Testings {
			get { return _testings; }
		}

		#region Load/Save

		public static Database Load(string path) {
			return Serializer.Load<Database>(path);
		}

		public void Save(string path) {
			Serializer.Save(path, this);
		}

		#endregion

		public void DeletePerson(Person person) {
			_procedures.Iterate(procedure => procedure.RemovePerson(person));
			_groups.Traverse(group => group.Groups).Iterate(group => group.Persons.Remove(person));
			_persons.Remove(person);
		}
	}
}
